import Const from '@/config'

export default {
	/* =============== 通用过滤器 ================ */
	valueFilter(val, map, to = 'text') {
		let arr = map.split('.')
		let MAP = Const[arr[0]][arr[1]] || {}
		let ITEM = MAP[val + ''] || {}
		return ITEM[to] || ''
	},
	imageFilter(item, default_type = 1) {
		if (!item || typeof item !== 'string') {
			// console.warn("imageFilter 没有找到图像")
			let map = Const.DEFULT_IMG
			return map[default_type] || ''
		}

		if (item.includes('http')) {
			return item
		} else {
			return Const.NET.FILE_URL_PREFIX + item
		}
	},

	addressFilter(obj, lang, type = 1) {
		if (typeof obj !== 'object' || !obj) {
			return '-'
		}
		let { province, city, county, address } = obj
		if (!province && !city && !county && !address) {
			return '-'
		}
		/* switch (type) {
                case 1:
                    return `${province} ${city} ${county} ${address}`;
                case 2:
                    return `${province} ${city} ${county}`;
            }*/
		if (lang === 'en') {
			let { province_en, city_en, county, address } = obj
			switch (type) {
				case 1:
					return `${province_en} ${city_en} ${county} ${address}`
			}
		}
		if (lang === 'zh') {
			let { province, city, county, address } = obj
			switch (type) {
				case 1:
					return `${province} ${city} ${county} ${address}`
				case 2:
					return `${province} ${city} ${county}`
			}
		}
	},

	boolFlagFilter(val, name) {
		if (name) {
			switch (val) {
				case 10:
					return '已' + name
				case 0:
					return '未' + name
				default:
					return '未知'
			}
		} else {
			switch (val) {
				case 10:
					return '是'
				case -10:
					return '否'
				default:
					return '未知'
			}
		}
	},
	tableFilterFormat(list, lang) {
		let arr = list.map(i => {
			return {
				value: i.value,
				text: i[lang],
			}
		})
		console.log('tableFilterFormat', arr)
		return arr
	},
	tableFieldsFilter(columns = [], role) {
		if (!columns.length) {
			return []
		}
		return columns.filter(item => {
			return item.role === undefined || item.role.includes(role)
		})
	},
	ifPhoneFilter(val) {
		let phoneReg = /^1(3|4|5|6|7|8|9)\d{9}$/
		if (phoneReg.test(val)) {
			return true
		} else {
			return false
		}
	},
	/**
	 * @description:数值转换 将后端传来的数值转换成对应值
	 * @param {Number} val 被除数 需要转换的数值
	 * @param {Number} divisor 除数 默认为100
	 * @param {Number} dp 保留几位小数 默认为2
	 * @param {Number} type 乘法/除法 false=除法 true=乘法
	 */
	countFilter(val = 0, divisor = 100) {
		if (val === 0 || val === '') {
			return '-'
		}

		let result = parseInt(val / 100)

		return result
	},

	/* =============== 通用过滤器 ================ */
}
